//
//  ThirdpartyLoginTableViewController.m
//  17DianYing
//
//  Created by 赵雄 on 15/9/19.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "ThirdpartyLoginTableViewController.h"
#import "UMSocial.h"
#import "AFNetworking.h"

@interface ThirdpartyLoginTableViewController ()

@end

@implementation ThirdpartyLoginTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getThirdpartyLogin];
 
}
-(void)getThirdpartyLogin{
    
    NSString * url =[http stringByAppendingString:@"/mobile/thirdparty/getThirdPartyStatus"];
    
    NSUserDefaults *to = [NSUserDefaults standardUserDefaults];
    NSString *token = [to objectForKey:@"usertoken"];
    
    NSLog(@"++++++++++++++++%@",token);
    NSDictionary * dic =  @{@"token":token};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    
    [manager GET:url  parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * d=operation.responseObject;
        NSString *QQ = [NSString stringWithFormat:@"%@",d[@"QQ"]];
        NSString *WEIBO = [NSString stringWithFormat:@"%@",d[@"WEIBO"]];
        NSString *WEIXIN = [NSString stringWithFormat:@"%@",d[@"WEIXIN"]];
        NSLog(@"%@",WEIBO);
        if ([QQ isEqualToString:@"1"]) {
            [self.switchbutton setOn:YES animated:NO];
            self.labelOne.text = @"已绑定";
            self.labelOne.textColor = [UIColor blueColor];
        }
        if ([WEIXIN isEqualToString:@"1"]) {
            [self.switchButtonTow setOn:YES animated:NO];
            self.labelTow.text = @"已绑定";
            self.labelOne.textColor = [UIColor blueColor];
        }
        if ([WEIBO isEqualToString:@"1"]) {
            [self.switchThree setOn:YES animated:NO];
            self.labelThree.text = @"已绑定";
            self.labelThree.textColor = [UIColor blueColor];
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
//    // Return the number of sections.
//    return 1;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
//    // Return the number of rows in the section.
//    return 3;
//}
//按钮1
- (IBAction)switchbutton:(id)sender {
    
    if ([self.switchbutton isOn]) {
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            //          获取微博用户名、uid、token等
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
                
                NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
                
                
                //储存
                //将上述数据全部存储到NSUserDefaults中
                NSUserDefaults *userSina = [NSUserDefaults standardUserDefaults];
                
                //存储时，除NSNumber类型使用对应的类型意外，其他的都是使用setObject:forKey:
                NSString *username = snsAccount.userName;
                NSString *uid = snsAccount.usid;
                NSString *urltow = snsAccount.iconURL;
                NSString *platformName = @"QQ";
                NSString *token = [userSina objectForKey:@"usertoken"];
                
                //我们使用AFHTTPRequestSerializer的POST提交方式
                AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
                manager.responseSerializer = [AFHTTPResponseSerializer serializer];
                NSString * url =[http stringByAppendingString:@"/mobile/thirdparty/bind"];
                NSDictionary *dic = @{@"uid":uid,@"thirdPartyType":platformName,@"nickName":username,@"picUrl":urltow,@"token":token};
                [manager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
                    
                    id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
                    NSDictionary *dic = obj;
                    
                    NSLog(@"&&&&&&&&&&&&&&&&%@",dic);
                    NSString *str = [NSString stringWithFormat:@"%@",dic[@"status"]];
                    
                    if ([str isEqualToString:@"40017"]) {
                        [self.switchbutton setOn:YES animated:YES];
                        self.labelOne.text = @"已绑定";
                        self.labelOne.textColor = [UIColor blueColor];
                        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"绑定成功" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                        [a show];
                    }else{
                        [self.switchbutton setOn:NO animated:YES];
                        self.labelOne.text = @"未绑定";
                        self.labelOne.textColor = [UIColor blueColor];
                        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"绑定未成功" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                        [a show];
                    }
                    
                    
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    NSLog(@"_________________________%@",error.localizedDescription);
                   
                }];
            }
            [self.switchbutton setOn:NO animated:YES];
            self.labelOne.text = @"未绑定";
            self.labelOne.textColor = [UIColor grayColor];
            UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"绑定未成功" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [a show];
        });
        
        
    }else {
        [self.switchbutton setOn:YES];
//        self.labelOne.textColor = [UIColor blueColor];
        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"无法解除绑定" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [a show];
        NSLog(@"no");
    }
    

    
}

//按钮2
- (IBAction)switchbuttonTow:(id)sender {
        
    if ([self.switchButtonTow isOn]) {
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
                
                NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
                
                
                //储存
                //将上述数据全部存储到NSUserDefaults中
                NSUserDefaults *userSina = [NSUserDefaults standardUserDefaults];
                
                //存储时，除NSNumber类型使用对应的类型意外，其他的都是使用setObject:forKey:
                NSString *username = snsAccount.userName;
                NSString *uid = snsAccount.usid;
                NSString *urltow = snsAccount.iconURL;
                NSString *platformName = @"WEIXIN";
                [userSina setObject:username forKey:@"username"];
                [userSina setObject:uid forKey:@"uid"];
                [userSina setObject:urltow forKey:@"url"];
                [userSina setObject:platformName forKey:@"platformName"];
                NSString *token = [userSina objectForKey:@"usertoken"];
                
                //我们使用AFHTTPRequestSerializer的POST提交方式
                AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
                manager.responseSerializer = [AFHTTPResponseSerializer serializer];
                NSString * url =[http stringByAppendingString:@"/mobile/thirdparty/getThirdPartyStatus"];
                NSDictionary *dic = @{@"uid":uid,@"thirdPartyType":platformName,@"nickName":username,@"picUrl":url,@"token":token};
                [manager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
                    
                    id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
                    NSDictionary *dic = obj;
                    
                   
                    NSString *str = [NSString stringWithFormat:@"%@",dic[@"status"]];
                    
                    if ([str isEqualToString:@"40017"]) {
                        [self.switchButtonTow setOn:YES animated:NO];
                        self.labelTow.text = @"已绑定";
                        self.labelTow.textColor = [UIColor blueColor];
                        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"绑定成功" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                        [a show];
                    }else{
                        [self.switchButtonTow setOn:NO animated:NO];
                        self.labelTow.text = @"未绑定";
                        self.labelTow.textColor = [UIColor blueColor];
                        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"绑定未成功" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                        [a show];
                    }
                    
                }
                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                          NSLog(@"_________________________%@",error.localizedDescription);
                          
                          
                      }];
                
            }
            [self.switchButtonTow setOn:NO animated:YES];
            self.labelTow.text = @"未绑定";
            self.labelTow.textColor = [UIColor grayColor];
            UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"绑定未成功" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [a show];
        });
        
    }else {
        [self.switchButtonTow setOn:YES];
        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"无法解除绑定" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [a show];
        NSLog(@"no");
    }
    
}

//按钮3
- (IBAction)switchThree:(id)sender {

    
    if ([self.switchThree isOn]) {
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            //          获取微博用户名、uid、token等
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
                
                NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
                
                //储存
                //将上述数据全部存储到NSUserDefaults中
                NSUserDefaults *userSina = [NSUserDefaults standardUserDefaults];
                
                //存储时，除NSNumber类型使用对应的类型意外，其他的都是使用setObject:forKey:
                NSString *username = snsAccount.userName;
                NSString *uid = snsAccount.usid;
                NSString *urltow = snsAccount.iconURL;
               ;
                NSString *platformName = @"WEIBO";
                [userSina setObject:username forKey:@"username"];
                [userSina setObject:uid forKey:@"uid"];
                [userSina setObject:urltow forKey:@"url"];
                [userSina setObject:platformName forKey:@"platformName"];
                NSString *token = [userSina objectForKey:@"usertoken"];
                
                [userSina synchronize];
                //我们使用AFHTTPRequestSerializer的POST提交方式
                AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
                manager.responseSerializer = [AFHTTPResponseSerializer serializer];
                NSString * url =[http stringByAppendingString:@"/mobile/thirdparty/bind"];
                NSDictionary *dic = @{@"uid":uid,@"thirdPartyType":platformName,@"nickName":username,@"picUrl":urltow,@"token":token};
                [manager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
                    
                    id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
                    NSDictionary *dic = obj;
                    
                    NSString *str = [NSString stringWithFormat:@"%@",dic[@"status"]];
                    
                    if ([str isEqualToString:@"40017"]) {
                        [self.switchThree setOn:YES animated:YES];
                        self.labelThree.text = @"已绑定";
                        self.labelThree.textColor = [UIColor blueColor];
                        
                        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"绑定成功" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                        [a show];
                    }else{
                        [self.switchThree setOn:NO animated:YES];
                        self.labelThree.text = @"未绑定";
                        self.labelThree.textColor = [UIColor grayColor];
                        
                        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"绑定未成功" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                        [a show];
                    }
                    
                    
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    NSLog(@"_________________________%@",error.localizedDescription);
                    
                }];
            }
            [self.switchThree setOn:NO animated:YES];
            self.labelThree.text = @"未绑定";
            self.labelThree.textColor = [UIColor grayColor];
            UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"绑定未成功" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [a show];
        });
        
    }else {
        [self.switchThree setOn:YES];
        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"无法解除绑定" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [a show];
       
    }

}

//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QQCell" forIndexPath:indexPath];
//    
//    // Configure the cell...
//    
//    return cell;
//}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
